package Action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.noticedao;
import Entity.Notice;
import Entity.User;
import mapper.Noticemapper;

/**
 * Servlet implementation class Notice1Servlet
 */
@WebServlet("/Notice1Servlet")
public class Notice1Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Notice1Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int page = 1;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String id =user.getUid();
		Noticemapper nm = new noticedao();
		List<Notice> notices =new ArrayList<Notice>(); 
		if(nm.queryByPage(id, page)!=null)
		{
			notices=nm.queryByPage(id, page);
		}
		for(int i=0;i<notices.size();i++){
			String le=notices.get(i).getContent();
			if(le.length()>50){
				le=le.substring(0,50);
				le=le+"...";
				notices.get(i).setContent(le);
			}
		}
		
		request.setAttribute("notices",notices);
		int Utype=user.getUtype();
		if(Utype==2 ){
		request.getRequestDispatcher("t_link.jsp").forward(request,response);
		}else if(Utype==3){
			request.getRequestDispatcher("s_link.jsp").forward(request,response);
		}
	}

}
